<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>demo 2048</title>
  <style>
    .box {
      width: 100px;
      height: 100px;
      margin: 10px;
      border: 1px solid #000;
      display: inline-block;
      line-height: 100px;
      text-align: center;
    }
  </style>
</head>

<body>
  <div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
  <div class="btn-list">
    <button onclick="goLeft()">Left</button>
    <button onclick="goRight()">Right</button>
    <button onclick="blink()">Blink</button>
  </div>
</body>

<script>
  function goLeft() {
    console.log("click left");
  }

  function goRight() {
    console.log("click right");
  }

  // 在数组中随机生成2或4
  function blink(arr) {
    console.log("生成一个2或4");
    var randomNumber = Math.random()
    randomNumber *= 16
    var index = Math.floor(randomNumber)
    var randomNumber = Math.random() // [0, 1)
    var seed
    if (randomNumber < 0.5) {
      seed = 2
    } else {
      seed = 4
    }
    arr[index] = seed
    render(arr)
  }

  // 把数组数字渲染到页面上
  function render(arr) {
    var boxs = document.getElementsByClassName("box");
    for (var i = 0; i < boxs.length; i++) {
      boxs[i].innerHTML = arr[i]
    }
  }

  // render([0, 0, 0, 0])
  blink([0,0,0,0])
</script>

</html>